package net.adetech.framework.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public JSONObject getMonthJson(Date date) {
        JSONObject result = new JSONObject();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        result.put("year", year);
        result.put("month", month);
        result.put("monthName", year + "年" + month + "月");

        return result;
    }


    private static int[] TIME_PEROID = {9, 10, 11, 14, 15, 16};
    private static List<String> workingDayList = new ArrayList<String>();
    private static List<String> holidayList = new ArrayList<String>();

    static {
        workingDayList.add("20180428");
        workingDayList.add("20180929");
        workingDayList.add("20180930");

        holidayList.add("20180429");
        holidayList.add("20180430");
        holidayList.add("20180501");
        holidayList.add("20180616");
        holidayList.add("20180617");
        holidayList.add("20180618");
        holidayList.add("20180922");
        holidayList.add("20180923");
        holidayList.add("20180924");
        holidayList.add("20181001");
        holidayList.add("20181002");
        holidayList.add("20181003");
        holidayList.add("20181004");
        holidayList.add("20181005");
        holidayList.add("20181006");
        holidayList.add("20181007");

    }


    public static JSONArray getDayArray(Date start, int offset) {

        String startDayId = new SimpleDateFormat("yyyyMMdd").format(start);
        String monthBeginDayId = startDayId.substring(0,6)+"01";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.add(Calendar.DATE,offset);
        Date end = calendar.getTime();
        String endDayId = new SimpleDateFormat("yyyyMMdd").format(end);
        calendar.add(Calendar.MONTH,1);
        calendar.set(Calendar.DAY_OF_MONTH,0);
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        Date monthEndDay = calendar.getTime();
        long endTime = monthEndDay.getTime();

        JSONArray monthArray =new JSONArray();
        Map<String,JSONObject> monthMap = new LinkedHashMap<String, JSONObject>();

        try {
            Date monthBeginDate = new SimpleDateFormat("yyyyMMdd").parse(monthBeginDayId);
            calendar.setTime(monthBeginDate);
            while (calendar.getTimeInMillis()<endTime)
            {
                Date tempDate = calendar.getTime();
                String dayId = new SimpleDateFormat("yyyyMMdd").format(tempDate);
                String monthId = new SimpleDateFormat("yyyyMM").format(tempDate);
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH)+1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                String monthName = year+"年"+month+"月";
                String dayName = year+"年"+month+"月"+day+"日";


                JSONObject monthObject;
                if (monthMap.keySet().contains(monthId))
                {
                    monthObject = monthMap.get(monthId);

                }
                else {
                    monthObject = new JSONObject();
                    monthObject.put("year",year);
                    monthObject.put("month",month);
                    monthObject.put("monthId",monthId);
                    monthObject.put("monthName",monthName);
                    monthObject.put("dayList",new JSONArray());
                    monthObject.put("firstDayOfWeek",dayOfWeek);
                    monthObject.put("appFlag",false);
                    JSONArray blankDay = new JSONArray();
                    for(int k=0;k<(dayOfWeek+5)%7;k++)
                    {
                        blankDay.add(k);
                    }
                    monthObject.put("blankDayList",blankDay);



                    monthArray.add(monthObject);
                    monthMap.put(monthId,monthObject);
                }
                JSONArray dayList = monthObject.getJSONArray("dayList");

                JSONObject dayJson = new JSONObject();
                dayJson.put("dayId",dayId);
                dayJson.put("day",day);
                dayJson.put("dayName",dayName);


                //不在区间内
                if (Integer.parseInt(dayId)<Integer.parseInt(startDayId)||Integer.parseInt(dayId)>Integer.parseInt(endDayId))
                {
                    dayJson.put("appFlag",false);
                }
                else {
                    //法定节假日
                    if(holidayList.contains(dayId)) {
                        dayJson.put("appFlag", false);
                    }
                    //不是周天或者是调休日
                    else if(dayOfWeek!=1||workingDayList.contains(dayId)){

                        JSONArray timePeriodArray = new JSONArray();
                        //如果是今天
                        if(dayId.equals(startDayId))
                        {
                            if(dayOfWeek==7&&!workingDayList.contains(dayId))
                            {
                                for(int j=0;j<3;j++)
                                {
                                    if (hour<TIME_PEROID[j])
                                    {
                                        timePeriodArray.add(TIME_PEROID[j]);
                                    }
                                }
                            }
                            else {
                                for(int j=0;j<TIME_PEROID.length;j++)
                                {
                                    if (hour<TIME_PEROID[j])
                                    {
                                        timePeriodArray.add(TIME_PEROID[j]);
                                    }
                                }
                            }
                        }
                        //如果是周六并且不是调休日
                        else if(dayOfWeek==7&&!workingDayList.contains(dayId))
                        {
                            for(int j=0;j<3;j++)
                            {
                                timePeriodArray.add(TIME_PEROID[j]);
                            }
                        }
                        //正常工作日
                        else {
                            for(int j=0;j<TIME_PEROID.length;j++)
                            {
                                timePeriodArray.add(TIME_PEROID[j]);
                            }
                        }
                        if (timePeriodArray.size()>0)
                        {
                            monthObject.put("appFlag",true);
                            dayJson.put("appFlag", true);
                            dayJson.put("timeList",timePeriodArray);
                        }
                        else {
                            dayJson.put("appFlag", false);
                        }


                    }
                    else {
                        dayJson.put("appFlag", false);
                    }

                }





                dayList.add(dayJson);
                calendar.add(Calendar.DAY_OF_MONTH,1);
            }


        }catch (Exception e)
        {
            e.printStackTrace();
        }

        for (int i=0;i<monthArray.size();i++)
        {
            if (i==0)
            {
                monthArray.getJSONObject(i).put("hasPrev",false);
            }else {
                monthArray.getJSONObject(i).put("hasPrev",true);
            }

            if (i>=monthArray.size()-1)
            {
                monthArray.getJSONObject(i).put("hasNext",false);
            }else {
                monthArray.getJSONObject(i).put("hasNext",true);
            }
        }

        return monthArray;
    }

    public static Date getFirstDayOfMonth(Date date)
    {
        try {
            return new SimpleDateFormat("yyyyMMdd").parse(new SimpleDateFormat("yyyyMM01").format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
